package com.ruoyi.web.controller.tool;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.web.service.TokenService;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Coordinate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;

/**
 * swagger 用户测试方法
 * 
 * @author ruoyi
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/api")
public class TestController extends BaseController
{
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "测试")
    @GetMapping("/test")
    public AjaxResult test() {

        return AjaxResult.success();
    }

    @ApiOperation(value = "测试生成图片")
    @GetMapping("/image")
    public AjaxResult generateImage() {
        try {
            BufferedImage img1 = Thumbnails.of("C:\\Users\\Administrator\\Desktop\\logo\\img_logo2@3x.png")
                    .size(688, 358)
                    .asBufferedImage();
            BufferedImage img2 = Thumbnails.of("C:\\Users\\Administrator\\Desktop\\logo\\img_logo1@3x.png")
                    .size(688, 358)
                    .asBufferedImage();
            BufferedImage img3 = Thumbnails.of("C:\\Users\\Administrator\\Desktop\\logo\\footer_logo@3x.png")
                    .size(378, 47)
                    .asBufferedImage();
            Thumbnails.of("C:\\Users\\Administrator\\Desktop\\logo\\bg_comprehensive@3x.png")
                    .size(4096, 2160)
                    .watermark(new Coordinate(80, 452), img2, 1f)
                    .watermark(new Coordinate(828, 452), img1, 1f)
                    .watermark(new Coordinate(1576, 452), img2, 1f)
                    .watermark(new Coordinate(2324, 452), img1, 1f)
                    .watermark(new Coordinate(3072, 452), img1, 1f)
                    .watermark(new Coordinate(80, 972), img2, 1f)
                    .watermark(new Coordinate(828, 972), img1, 1f)
                    .watermark(new Coordinate(1576, 972), img2, 1f)
                    .watermark(new Coordinate(2324, 972), img1, 1f)
                    .watermark(new Coordinate(3072, 972), img1, 1f)
                    .watermark(new Coordinate(80, 1490), img2, 1f)
                    .watermark(new Coordinate(828, 1490), img1, 1f)
                    .watermark(new Coordinate(1576, 1490), img2, 1f)
                    .watermark(new Coordinate(2324, 1490), img1, 1f)
                    .watermark(new Coordinate(3072, 1490), img1, 1f)
                    .watermark(new Coordinate(1731, 2038), img3, 1f)
                    .toFile("C:\\Users\\Administrator\\Desktop\\logo\\"+ IdUtils.fastSimpleUUID() + ".png");
        } catch (IOException e) {
            e.printStackTrace();
        }


        return AjaxResult.success();
    }

    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
    {
        users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
        users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    public AjaxResult userList()
    {
        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
        return AjaxResult.success(userList);
    }

    @ApiOperation("获取用户详细")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
    @GetMapping("/{userId}")
    public AjaxResult getUser(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            return AjaxResult.success(users.get(userId));
        }
        else
        {
            return error("用户不存在");
        }
    }

    @ApiOperation("新增用户")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer"),
        @ApiImplicitParam(name = "username", value = "用户名称", dataType = "String"),
        @ApiImplicitParam(name = "password", value = "用户密码", dataType = "String"),
        @ApiImplicitParam(name = "mobile", value = "用户手机", dataType = "String")
    })
    @PostMapping("/save")
    public AjaxResult save(UserEntity user)
    {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
        {
            return error("用户ID不能为空");
        }
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("更新用户")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody UserEntity user)
    {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
        {
            return error("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId()))
        {
            return error("用户不存在");
        }
        users.remove(user.getUserId());
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
    @DeleteMapping("/{userId}")
    public AjaxResult delete(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            users.remove(userId);
            return success();
        }
        else
        {
            return error("用户不存在");
        }
    }
}

@ApiModel(value = "UserEntity", description = "用户实体")
class UserEntity
{
    @ApiModelProperty("用户ID")
    private Integer userId;

    @ApiModelProperty("用户名称")
    private String username;

    @ApiModelProperty("用户密码")
    private String password;

    @ApiModelProperty("用户手机")
    private String mobile;

    public UserEntity()
    {

    }

    public UserEntity(Integer userId, String username, String password, String mobile)
    {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.mobile = mobile;
    }

    public Integer getUserId()
    {
        return userId;
    }

    public void setUserId(Integer userId)
    {
        this.userId = userId;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getMobile()
    {
        return mobile;
    }

    public void setMobile(String mobile)
    {
        this.mobile = mobile;
    }
}



